using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using System;
using System.Windows;
using System.Windows.Media.Imaging;
namespace MicroMsg.Manager
{
	public class HeadImgMgr
	{
		private const string TimeStampHeadImg = "dblLastGetQQHeadImgTimeStamp";
		private const int MIN_CLEAN_INTERVAL = 86400000;
		public static BitmapImage defaultHeadImage
		{
			get
			{
				return HeadImageMgr.defaultHeadImage;
			}
		}
		public static BitmapImage defaultQQHeadImage
		{
			get
			{
				return HeadImageMgr.defaultQQHeadImage;
			}
		}
		public static string getUserName(object userInfo)
		{
			if (userInfo == null)
			{
				return null;
			}
			if (userInfo is Contact)
			{
				return ((Contact)userInfo).strUsrName;
			}
			if (userInfo is QFriend)
			{
				return HeadImgMgr.qq2strUserName(((QFriend)userInfo).nQQ);
			}
			if (userInfo is LbsPeople)
			{
				return ((LbsPeople)userInfo).strUserName;
			}
			if (userInfo is ShakePeople)
			{
				return ((ShakePeople)userInfo).strUserName;
			}
			if (userInfo is BottleContact)
			{
				return ((BottleContact)userInfo).strUserName;
			}
			if (userInfo is string)
			{
				return userInfo as string;
			}
			return null;
		}
		private static BitmapImage getDefalutHeadImage(string strUsrName)
		{
			if (!ContactHelper.isQContact(strUsrName))
			{
				return HeadImageMgr.defaultHeadImage;
			}
			return HeadImageMgr.defaultQQHeadImage;
		}
		public static BitmapImage getCacheHeadImage(object userInfo)
		{
			string userName = HeadImgMgr.getUserName(userInfo);
			if (string.IsNullOrEmpty(userName))
			{
				return HeadImgMgr.defaultHeadImage;
			}
			BitmapImage cacheHeadImage = HeadImageMgr.getCacheHeadImage(userName);
			if (cacheHeadImage != null)
			{
				return cacheHeadImage;
			}
			return HeadImgMgr.getDefalutHeadImage(userName);
		}
		public static BitmapImage getHeadImage(string strUsrName, bool bAsync = true)
		{
			if (bAsync)
			{
				return HeadImgMgr.asyncGetHeadImage(strUsrName);
			}
			return HeadImgMgr.syncGetHeadImage(strUsrName);
		}
		private static BitmapImage asyncGetHeadImage(string strUsrName)
		{
			BitmapImage bitmapImage_0 = HeadImageMgr.getCacheHeadImage(strUsrName);
			if (bitmapImage_0 != null)
			{
				return bitmapImage_0;
			}
			Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
			{
				bitmapImage_0 = HeadImageMgr.getHeadImage(strUsrName);
				if (bitmapImage_0 != null)
				{
					HeadImageMgr.getImageBroadCasting(strUsrName, bitmapImage_0);
					if (HeadImageMgr.isHeadImgVersionChange(strUsrName))
					{
						Log.smethod_5("HeadImgMgr", "head img version change " + strUsrName);
						HeadImageMgr.downLoadHeadImage(strUsrName, null);
						StorageMgr.hdheadimg.method_0(strUsrName);
						return;
					}
				}
				else
				{
					HeadImageMgr.downLoadHeadImage(strUsrName, null);
				}
			});
			return HeadImgMgr.getDefalutHeadImage(strUsrName);
		}
		private static BitmapImage syncGetHeadImage(string strUsrName)
		{
			BitmapImage headImage = HeadImageMgr.getHeadImage(strUsrName);
			if (headImage != null)
			{
				if (HeadImageMgr.isHeadImgVersionChange(strUsrName))
				{
					Log.smethod_5("HeadImgMgr", "head img version change " + strUsrName);
					HeadImageMgr.downLoadHeadImage(strUsrName, null);
					StorageMgr.hdheadimg.method_0(strUsrName);
				}
				return headImage;
			}
			HeadImageMgr.downLoadHeadImage(strUsrName, null);
			return HeadImgMgr.getDefalutHeadImage(strUsrName);
		}
		public static BitmapImage getHeadImage(object object_0)
		{
			if (object_0 is Contact)
			{
				return HeadImgMgr.getHeadImage(object_0 as Contact);
			}
			if (object_0 is QFriend)
			{
				return HeadImgMgr.getHeadImage(object_0 as QFriend);
			}
			if (object_0 is ShakePeople)
			{
				return HeadImgMgr.getHeadImage(object_0 as ShakePeople);
			}
			if (object_0 is LbsPeople)
			{
				return HeadImgMgr.getHeadImage(object_0 as LbsPeople);
			}
			if (object_0 is BottleContact)
			{
				return HeadImgMgr.getHeadImage(object_0 as BottleContact);
			}
			if (object_0 is string)
			{
				return HeadImgMgr.getHeadImage(object_0 as string, true);
			}
			return null;
		}
		public static BitmapImage getHeadImage(Contact item)
		{
			if (item == null)
			{
				return HeadImageMgr.defaultHeadImage;
			}
			return HeadImgMgr.asyncGetHeadImage(item.strUsrName);
		}
		private static BitmapImage getHeadImage(QFriend item)
		{
			if (item == null)
			{
				return HeadImageMgr.defaultQQHeadImage;
			}
			return HeadImgMgr.asyncGetHeadImage(HeadImgMgr.qq2strUserName(item.nQQ));
		}
		public static BitmapImage getHeadImage(ShakePeople item)
		{
			if (item == null)
			{
				return HeadImgMgr.getDefalutHeadImage(item.strUserName);
			}
			HeadImageMgr.setHasHeadImage(item.strUserName, item.bHasImg);
			HDheadImgMgr.setHasHDheadImg(item.strUserName, item.bHasHDImg);
			HeadImageMgr.setHeadImageVersion(item.strUserName, item.nHeadImgVersion);
			if (!item.bHasImg)
			{
				return HeadImgMgr.getDefalutHeadImage(item.strUserName);
			}
			return HeadImgMgr.asyncGetHeadImage(item.strUserName);
		}
		public static BitmapImage getHeadImage(LbsPeople item)
		{
			if (item == null)
			{
				return HeadImgMgr.getDefalutHeadImage(item.strUserName);
			}
			HeadImageMgr.setHasHeadImage(item.strUserName, item.bHasImg);
			HeadImageMgr.setHeadImageVersion(item.strUserName, item.nHeadImgVersion);
			if (!item.bHasImg)
			{
				return HeadImgMgr.getDefalutHeadImage(item.strUserName);
			}
			return HeadImgMgr.asyncGetHeadImage(item.strUserName);
		}
		public static BitmapImage getHeadImage(BottleContact item)
		{
			if (item == null || string.IsNullOrEmpty(item.strUserName))
			{
				return HeadImgMgr.defaultHeadImage;
			}
			HeadImageMgr.setHasHeadImage(item.strUserName, item.nImgFlag != 4);
			HDheadImgMgr.setHasHDheadImg(item.strUserName, item.nHDImgFlag == 1);
			if (item.nImgFlag == 4)
			{
				return HeadImgMgr.getDefalutHeadImage(item.strUserName);
			}
			return HeadImgMgr.asyncGetHeadImage(item.strUserName);
		}
		public static string qq2strUserName(uint nQQ)
		{
			return nQQ + "@qqim";
		}
		public static BitmapImage tryGetNowQQHeadImage(QFriend item)
		{
			if (item == null)
			{
				return HeadImageMgr.defaultQQHeadImage;
			}
			return HeadImgMgr.tryGetNowQQHeadImage(HeadImgMgr.qq2strUserName(item.nQQ));
		}
		public static BitmapImage tryGetNowQQHeadImage(string qqUsrName)
		{
			if (string.IsNullOrEmpty(qqUsrName))
			{
				return HeadImageMgr.defaultQQHeadImage;
			}
			double nowMilliseconds = Util.getNowMilliseconds();
			if (nowMilliseconds > ConfigMgr.getDouble("dblLastGetQQHeadImgTimeStamp", 0.0) + 86400000.0)
			{
				HeadImageMgr.update(qqUsrName);
				ConfigMgr.setDouble("dblLastGetQQHeadImgTimeStamp", nowMilliseconds);
			}
			return HeadImgMgr.getHeadImage(qqUsrName, true);
		}
	}
}
